In [23]:
from IPython.display import IFrame
import pandas as pd
from bokeh.models import ColumnDataSource, Plot, Circle, Range1d, LinearAxis, TapTool, HoverTool, Text
from bokeh.plotting import output_notebook, show
output_notebook()
In [24]:
IFrame(src="https://www.youtube.com/embed/hVimVzgtD6w?t=3m57s", width=420, height=315)
Out[24]:
For the impatient, skip to 3m 58s. Otherwise enjoy a great example of statistics communication.
In [25]:
# Links via http://www.gapminder.org/data/
population_url = "http://spreadsheets.google.com/pub?key=phAwcNAVuyj0XOoBL_n5tAQ&output=xls"
fertility_url = "http://spreadsheets.google.com/pub?key=phAwcNAVuyj0TAlJeCEzcGQ&output=xls"
life_expectancy_url = "http://spreadsheets.google.com/pub?key=tiAiXcrneZrUnnJ9dBU-PAw&output=xls"
In [4]:
def get_data(url):
# Get the data from the url and return only 1962 - 2013
df = pd.read_excel(url, index_col=0)
df = df.unstack().unstack()
df = df[(df.index >= 1962) & (df.index <= 2013)]
df = df.unstack().unstack()
return df
fertility_df = get_data(fertility_url)
life_expectancy_df = get_data(life_expectancy_url)
In [5]:
fertility_df = fertility_df.drop(fertility_df.index.difference(life_expectancy_df.index))
In [27]:
sources = []
for country in fertility_df.index:
fertility = fertility_df.loc[country]
fertility.name = 'fertility'
life = life_expectancy_df.loc[country]
life.name = 'life'
new_df = pd.concat([fertility, life], axis=1)
new_df['country'] = country
sources.append(ColumnDataSource(new_df))
In [28]:
years = list(fertility_df.columns)
In [47]:
xdr = Range1d(1, 8)
ydr = Range1d(20, 85)
plot = Plot(
x_range=xdr,
y_range=ydr,
title="",
plot_width=800,
plot_height=600,
outline_line_color=None,
toolbar_location=None
)
xaxis = LinearAxis()
yaxis = LinearAxis()
plot.add_layout(xaxis, 'left')
plot.add_layout(yaxis, 'below')
tooltips = "@index<br />@country"
plot.add_tools(HoverTool(tooltips=tooltips))
default_selection = {
'0d': {'flag': False, 'indices': []},
'1d': {'indices': [0]},
'2d': {'indices': []}
}
for source in sources:
source.selected = default_selection
empty_circle = Circle(x='fertility', y='life', fill_color=None, line_color=None, size=5)
red_circle = Circle(x='fertility', y='life', fill_color='red', line_color=None, size=5)
plot.add_glyph(source, empty_circle, selection_glyph=red_circle, nonselection_glyph=empty_circle)
empty_text = Text(x=2, y=30, text='index', text_color='white')
text = Text(x=2, y=30, text='index', text_color='black')
text_source = sources[0]
text_source.selected = default_selection
plot.add_glyph(text_source, empty_text, selection_glyph=text, nonselection_glyph=empty_text)
show(plot)
In [50]:
def update(year=1962):
new_selection = {
'0d': {'flag': False, 'indices': []},
'1d': {'indices': [years.index(year)]},
'2d': {'indices': []}
}
for source in sources:
source.selected = new_selection
source.push_notebook()
text_source.selected = new_selection
text_source.push_notebook()
In [51]:
from IPython.html.widgets import interact
interact(update, year=(1962,2013))
In [ ]: